package plugados.controle;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;

public class ConexaoBanco {

	public Statement stm;
	public ResultSet rs;
	private String driver = "org.mysql.Driver";
	private String caminho = "jdbc:mysql://localhost/projetoplugados";
	private String usuario = "root";
	private String senha = "luan";
	public Connection conn;
	
	public void conecta() {
		System.setProperty("jdbc.Drivers", driver);
		try {
			conn = DriverManager.getConnection(caminho, usuario, senha);
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, "Erro ao conectar com banco de dados!\n" +
												"Erro: " + e.getMessage());
		}
		
	}
	
	public void executaSQL(String sql) {
		try {
			stm = conn.createStatement(rs.TYPE_SCROLL_INSENSITIVE, rs.CONCUR_READ_ONLY);
			rs = stm.executeQuery(sql);
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, "Erro: " + e.getMessage());
		}
	}
	
	public void desconecta() {
		try {
			conn.close();
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, "Erro ao desconectar do banco de dados!\n" +
											"Erro: " +e.getMessage());
		}
	}
	
}
